<?php
/******************************************************************************
*                                                                             *
* Steak Desk v0.2                                                             *
*                                                                             *
* Copyright (c) 2011 Andrew D. Zonenberg.                                     *
* All rights reserved.                                                        *
*                                                                             *
* Redistribution and use in source and binary forms, with or without modifi-  *
* cation, are permitted provided that the following conditions are met:       *
*                                                                             *
*    * Redistributions of source code must retain the above copyright notice  *
*      this list of conditions and the following disclaimer.                  *
*                                                                             *
*    * Redistributions in binary form must reproduce the above copyright      *
*      notice, this list of conditions and the following disclaimer in the    *
*      documentation and/or other materials provided with the distribution.   *
*                                                                             *
*    * Neither the name of the author nor the names of its contributors may be*
*      used to endorse or promote products derived from this software without *
*      specific prior written permission.                                     *
*                                                                             *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED *
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN     *
* NO EVENT SHALL THE AUTHOR BE HELD LIABLE FOR ANY DIRECT, INDIRECT,          *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT*
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, *
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING        *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS          *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                *
*                                                                             *
*******************************************************************************/

/**
	@brief Determines if the current URL is related to the supplied URL
 */
function is_related($url)
{
	$parsed = parse_url($_SERVER['REQUEST_URI']);
	$base = basename($parsed['path']);
	if($base == '')
		return false;	//directory only
	
	$related = array(
		'inventory.php' => array('asset.php', 'wishlist.php')
		);
		
	if(substr_count($url, $base) > 0)
		return true;
		
	//map of related files
	if(isset($related[$url]))
	{
		$rels = $related[$url];
		foreach($rels as $rel)
		{
			if(substr_count($_SERVER['REQUEST_URI'], $rel) == 1)
				return true;
		}
	}
	
	return false;
}

/**
	@brief Gets the top menu
	
	@return Menu
 */
function topmenu()
{
	$str = "";
	
	if($_SESSION['uid'] < 0)
	{
		$menu = array(
			'index.php' => 'Overview',
			'projects.php' => 'Project Manager',
			);
	}
	else
	{
		$menu = array(
			'index.php' => 'Overview',
			'inventory.php' => 'Inventory',
			'projects.php' => 'Project Manager',
			'todo.php' => 'To Do',
			'user.php' => 'My Account',
			'admin.php' => 'Administration'
			);
	}
	
	foreach($menu as $url => $capt)
		$str .= topmenuitem($url, $capt). ' ';
	
	return $str;
}

/**
	@brief Gets the HTML for a menu item
 */
function topmenuitem($url, $str)
{
	global $g_tplvars;
	$g_tplvars['url'] = $url;
	$g_tplvars['str'] = $str;
	if(is_related($url))
		return templatize('../templates/topmenuitem-on.html');
	else
		return templatize('../templates/topmenuitem-off.html');
}

/**
	@brief Gets the side menu

	TODO: make this more efficient
	
	@return Menu
 */
function sidemenu()
{
	if(isset($_GET['id']))
		$id = intval($_GET['id']);
	else
		$id = -1;
	
	//Menu table
	$menus = array(
		'index.php' =>
			array(
				'index.php?action=intro' => 'Overview',
				),
		'inventory.php' =>
			array(
				'inventory.php?action=browse' => 'Product Index',
				'inventory.php?action=summary' => 'Inventory List',
				//'inventory.php?action=addinv' => 'Add to Inventory',
				'inventory.php?action=summary&alertonly=1' => 'Reorder Alerts',
				'inventory.php?action=orders' => 'View Orders',
				'inventory.php?action=datasheets' => 'Datasheets',
				'inventory.php?action=vendors' => 'Vendor List',
				//'inventory.php?action=createvendor' => 'Add Vendor',
				),
		'projects.php' =>
			array(
				'projects.php?action=summary' => 'All Projects',
				'projects.php?action=create' => 'New Project',
				'projects.php?action=detail&id=' . $id => 'Project Overview',
				'projects.php?action=issues&id=' . $id => 'View Issues',
				'projects.php?action=addissue&id=' . $id => 'New Issue',
				'projects.php?action=addrepo&id=' . $id => 'Add Repository'
				),
		'todo.php' =>
			array(
				'todo.php?action=summary' => 'Active Tasks',
				'todo.php?action=blocked' => 'Blocked Tasks',
				'todo.php?action=completed' => 'Done Tasks',
				'todo.php?action=create' => 'New Task'
				),
		);
		
	//This is not for security, as the actions are banned internally by the system
	//But we want to avoid showing users things they can't do to keep things simple
	$privateactions = array('add', 'delete', 'create', 'new');
	
	//Look up the right one to use
	$menu = NULL;
	$str = '';
	foreach($menus as $f => $arr)
	{
		if(is_related($f))
		{
			$menu = $arr;
			break;
		}
	}
	
	//Render the menu
	if($menu != NULL)
	{
		foreach($menu as $url => $capt)
		{
			if(substr_count($url, 'id=-1') > 0)
				continue;
			
			//Skip private actions if not logged in	
			if($_SESSION['uid'] < 0)
			{
				$ok = true;
				foreach($privateactions as $priv)
				{
					if(substr_count($url, $priv) > 0)
						$ok = false;
				}
				if(!$ok)
					continue;
			}
				
			$str .= sidemenuitem($url, $capt). ' ';
		}
	}
	if($_SESSION['uid'] < 0)
		$str .= sidemenuitem('index.php?action=login', 'Log In');
	else
		$str .= sidemenuitem('user.php?action=logout', 'Log Out');
	
	return $str;
}

/**
	@brief Gets the HTML for a menu item
 */
function sidemenuitem($url, $str)
{
	global $g_tplvars;
	$g_tplvars['url'] = $url;
	$g_tplvars['str'] = $str;
	if(substr_count($_SERVER['REQUEST_URI'], $url) == 1)
		return templatize('../templates/sidemenuitem-on.html');
	else
		return templatize('../templates/sidemenuitem-off.html');
}

?>
